﻿Public Class Equipement

    Dim db As New Connexion
    Dim action As String
    Dim Entrer As String
    Dim TextBoxID As String
    Dim Niveau As Integer = 1
    Dim LibelleNiv As String()
    Dim CodeNiv As String()
    Dim NomNiv As String()
    Dim IdNiv As String()
    Dim Nomtable As String()
    Dim NbrNiv As Integer = 0
    Dim idAct As String = ""
    Public databaseName As String

    Private Sub Equipement_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Entrer = ""
        DataGridViewEquip.MultiSelect = False
        DataGridViewEquip.SelectionMode = DataGridViewSelectionMode.FullRowSelect
        DataGridViewEquip.ReadOnly = True


        db.myReq = "SELECT COUNT(syscolumns.name) AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= 'InfraEquipTyp'"
        db.Connecter(databaseName)
        While db.rdr.Read()
            NbrNiv = db.rdr(0) - 7
        End While
        db.Disconnecter()
        LibelleNiv = New String(NbrNiv - 1) {}
        CodeNiv = New String(NbrNiv - 1) {}
        NomNiv = New String(NbrNiv - 1) {}
        IdNiv = New String(NbrNiv - 1) {}
        Nomtable = New String(NbrNiv + 7) {}
        Dim iter As Integer = 1
        While iter < NbrNiv
            db.myReq = "SELECT [Libelle_Niveau] FROM  [dbo].[Niveau] Where [Table_Niveau]='InfraEquipTyp' and [Niv_Niveau]='" & iter & "'"
            db.Connecter(databaseName)
            While db.rdr.Read()
                LibelleNiv(iter - 1) = db.rdr(0)
            End While
            db.Disconnecter()
            iter = iter + 1
        End While

        db.myReq = "SELECT syscolumns.name AS colonne FROM sysobjects , syscolumns WHERE  ( sysobjects.id = syscolumns.id) And  (sysobjects.xtype = 'U') and sysobjects.name= 'InfraEquipTyp'"
        db.Connecter(databaseName)
        iter = 0
        While db.rdr.Read()
            Nomtable(iter) = db.rdr(0)
            iter = iter + 1
        End While
        db.Disconnecter()
        majCatg()
        miseajour()
    End Sub




    Private Sub majCatg()
        Try
            Entrer = ""
            db.myReq = "Select * FROM  [dbo].[EquipCateg]"
            db.Connecter(databaseName)
            Me.ComboBoxCodeCatg.Items.Clear()
            Me.ComboBoxNomCatg.Items.Clear()
            Me.ComboBoxCodeCatg.Items.Add("")
            Me.ComboBoxNomCatg.Items.Add("Tous")

            While db.rdr.Read()
                Me.ComboBoxCodeCatg.Items.Add(db.rdr(0))
                Me.ComboBoxNomCatg.Items.Add(db.rdr(2))
            End While
            Me.ComboBoxCodeCatg.SelectedIndex = 0
            Me.ComboBoxNomCatg.SelectedIndex = 0
            db.Disconnecter()
            majTyp()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub
    Private Sub ComboBoxNomCatg_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxNomCatg.TextChanged

        If Me.ComboBoxNomCatg.FindString(Me.ComboBoxNomCatg.Text) > 0 Then
            Dim Pos As Int32 = Me.ComboBoxNomCatg.Text.Length
            Me.ComboBoxNomCatg.SelectedIndex = Me.ComboBoxNomCatg.FindString(Me.ComboBoxNomCatg.Text)
            Me.ComboBoxNomCatg.SelectionStart = Pos
            Me.ComboBoxNomCatg.SelectionLength = Me.ComboBoxNomCatg.Text.Length - Pos
            Me.ComboBoxCodeCatg.SelectedIndex = Me.ComboBoxNomCatg.FindString(Me.ComboBoxNomCatg.Text)
            Niveau = 1
            majTyp()
        Else
            Me.ComboBoxCodeCatg.Text = ""
        End If
    End Sub


    Private Sub majTyp()

        Dim requeteX As String
        Dim niv As Integer = 1


        requeteX = "Select * from  [dbo].[InfraEquipTyp] WHERE " & Nomtable(4 + NbrNiv) & " = '" & Niveau & "' and  " & Nomtable(1) & "='" & Me.ComboBoxCodeCatg.SelectedItem & "'"

        While niv < Niveau
            requeteX = requeteX & "and " & Nomtable(3 + niv) & " = '" & CodeNiv(niv - 1) & "' "
            niv = niv + 1
        End While


        If Niveau = 1 Then
            Me.ButtonBackAll.Enabled = False
            Me.ButtonNext.Enabled = True
        ElseIf Niveau = NbrNiv Then
            Me.ButtonBackAll.Enabled = True
            Me.ButtonNext.Enabled = False
        Else
            Me.ButtonBackAll.Enabled = True
            Me.ButtonNext.Enabled = True
        End If

        db.myReq = requeteX
        db.Connecter(databaseName)

        Me.ComboBoxCodeTyp.Items.Clear()
        Me.ComboBoxNomType.Items.Clear()
        While db.rdr.Read()
            Me.ComboBoxNomType.Items.Add(db.rdr(NbrNiv + 5))
            Me.ComboBoxCodeTyp.Items.Add(db.rdr(0))
        End While
        db.Disconnecter()

        If ComboBoxCodeTyp.Items.Count < 0 Then
            Me.ButtonNext.Enabled = False
        Else
            Me.ButtonNext.Enabled = True
        End If
    End Sub
    Private Sub ComboBoxNomType_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBoxNomType.TextChanged

        'If Me.ComboBoxNomType.FindString(Me.ComboBoxNomType.Text) > 0 Then
        Dim Pos As Int32 = Me.ComboBoxNomType.Text.Length
        Me.ComboBoxNomType.SelectedIndex = Me.ComboBoxNomType.FindString(Me.ComboBoxNomType.Text)
        Me.ComboBoxNomType.SelectionStart = Pos
        Me.ComboBoxNomType.SelectionLength = Me.ComboBoxNomType.Text.Length - Pos
        Me.ComboBoxCodeTyp.SelectedIndex = Me.ComboBoxNomType.FindString(Me.ComboBoxNomType.Text)

        Dim niv As Integer = 0
        Me.TextBoxDecoupageHab.Text = ""
        NomNiv(Niveau - 1) = Me.ComboBoxNomType.Text
        While niv < NbrNiv
            Me.TextBoxDecoupageHab.Text = Me.TextBoxDecoupageHab.Text & NomNiv(niv)
            niv = niv + 1
        End While

        IdNiv(Niveau - 1) = Me.ComboBoxCodeTyp.Text
        'Else
        ' Me.ComboBoxCodeTyp.Text = ""
        ' End If
    End Sub


    Private Sub ButtonNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNext.Click
        Entrer = ""
        Niveau = Niveau + 1
        Me.LabelNiv.Text = LibelleNiv(Niveau)

        db.myReq = "Select * from  [dbo].[InfraEquipTyp] WHERE " & Nomtable(0) & " = '" & Me.ComboBoxCodeTyp.Text & "'"
        db.Connecter(databaseName)
        While db.rdr.Read
            CodeNiv(Niveau - 2) = db.rdr(2 + Niveau)
        End While

        majTyp()
    End Sub
    Private Sub ButtonBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonBackAll.Click
        Dim niv As Integer = 0
        Me.TextBoxDecoupageHab.Text = ""
        Entrer = ""
        Niveau = Niveau - 1
        Me.LabelNiv.Text = LibelleNiv(Niveau - 1)
        While niv < Niveau
            Me.TextBoxDecoupageHab.Text = Me.TextBoxDecoupageHab.Text & NomNiv(niv)
            niv = niv + 1
        End While
        majTyp()
        Me.ComboBoxNomType.SelectedIndex = Me.ComboBoxNomType.Items.IndexOf(IdNiv(Niveau - 1))
    End Sub



    Private Sub miseajour()
        Try
            db.myReq = "SELECT * FROM  [dbo].[EQUIPEMENTSITE2]"
            db.Connecter(databaseName)
            Me.DataGridViewEquip.Rows.Clear()
            While db.rdr.Read()
                DataGridViewEquip.Rows.Add(db.rdr(0), db.rdr(1), db.rdr(2), db.rdr(3), db.rdr(4), db.rdr(5), db.rdr(6), db.rdr(7))
                idAct = db.rdr(0)
            End While
            db.Disconnecter()

            If DataGridViewEquip.RowCount - 1 = 0 Then
                Me.TextBoxLocal.Text = ""
                Me.TextBoxDesc.Text = ""
                Me.TextBoxInt.Text = ""
            Else
                DataGridViewEquip.Rows(0).Selected = True
                Me.TextBoxLocal.Text = DataGridViewEquip.SelectedCells(4).Value
                Me.TextBoxDesc.Text = DataGridViewEquip.SelectedCells(6).Value
                Me.TextBoxInt.Text = DataGridViewEquip.SelectedCells(3).Value
                Me.DateTimePickerD.Text = DataGridViewEquip.SelectedCells(5).Value
                Me.oidValue.Text = DataGridViewEquip.SelectedCells(7).Value
            End If

        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub

    Private Sub DataGridViewEquip_CurrentCellChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridViewEquip.CurrentCellChanged
        If Entrer = "debut" Then
            selection()
        End If
    End Sub
    Private Sub DataGridViewEquip_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridViewEquip.MouseClick
        If e.Button = MouseButtons.Left Then
            Entrer = "debut"
            selection()
        End If
    End Sub


    Private Sub selection()
        Try
            If Not DataGridViewEquip.SelectedCells(0).ToString = "" Then
                Me.TextBoxLocal.Text = DataGridViewEquip.SelectedCells(4).Value
                Me.TextBoxDesc.Text = DataGridViewEquip.SelectedCells(6).Value
                Me.TextBoxInt.Text = DataGridViewEquip.SelectedCells(3).Value
                Me.DateTimePickerD.Text = DataGridViewEquip.SelectedCells(5).Value
                Me.oidValue.Text = DataGridViewEquip.SelectedCells(7).Value
            End If
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
    End Sub

    Private Sub ButtonNouv_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNouv.Click
        action = "ajouter"
        Me.TextBoxLocal.Text = ""
        Me.TextBoxDesc.Text = ""
        Me.TextBoxInt.Text = ""
    End Sub
    Private Sub ButtonEnregistrer_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonEnregistrer.Click
        Try
            Entrer = ""
            If action = "ajouter" Then
                db.myReq = "INSERT INTO  [dbo].[EQUIPEMENTSITE2]([Id_TypEq],[ID_Sit],[NOMEQ_EQP],[LOCAL_EQP],[DATE_EQP] ,[EXPLIC_EQP],[SIGID]) VALUES('" & Me.ComboBoxCodeTyp.Text & "','" & db.idSite & "','" & Me.TextBoxInt.Text & "','" & Me.TextBoxLocal.Text & "','" & Me.DateTimePickerD.Text & "','" & Me.TextBoxDesc.Text & "','" & Me.oidValue.Text & "')"

            Else
                db.myReq = "UPDATE  [dbo].[EQUIPEMENTSITE2] SET [Id_TypEq] = '" & Me.ComboBoxCodeTyp.Text & "',[ID_Sit] = '" & db.idSite & "',[NOMEQ_EQP] = '" & Me.TextBoxInt.Text & "',[LOCAL_EQP] = '" & Me.TextBoxLocal.Text & "',[DATE_EQP] = '" & Me.DateTimePickerD.Text & "',[EXPLIC_EQP] = '" & Me.TextBoxDesc.Text & "',[SIGID]='" & Me.oidValue.Text & "' WHERE [Id_EqP]='" & Me.DataGridViewEquip.SelectedCells(0).Value & "')"
            End If
            db.Connecter(databaseName)
            db.Disconnecter()
        Catch ex As Exception
            MsgBox("Problème de connexion")
        End Try
        miseajour()
    End Sub

    Private Sub ButtonSup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonSup.Click
        Try
            db.myReq = " DELETE FROM  [dbo].[EQUIPEMENTSITE2] WHERE  [Id_EqP]='" & Me.DataGridViewEquip.SelectedCells(0).Value & "')"
            db.Connecter(databaseName)
            db.Disconnecter()
            miseajour()
            If DataGridViewEquip.RowCount - 1 = 0 Then
                Return
            End If
            Dim listOid As List(Of String) = New List(Of String)

            listOid.Add(DataGridViewEquip.SelectedCells(7).Value)

            _workspace.deleteNodes(_layerName, listOid)
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Dim typX As New TypologieNivX
        typX.Text = "Typologie"
        typX.Table1 = "EquipCateg"
        typX.Table2 = "InfraEquipTyp"
        typX.Visible = True
    End Sub

    Private Sub Button13_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button13.Click
        Dim typ1 As New TypologieNiv1Form(databaseName)
        typ1.Text = "Catégorie"
        typ1.Table = "EquipCateg"
        typ1.Visible = True
    End Sub


    ''' <summary>
    ''' ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ''' </summary>
    ''' <remarks></remarks>
    ''' 
    Private _workspace As Object
    Private _layerName As String
    Private geoForm As GeoLocation = Nothing
    Property layerName() As String
        Get
            Return _layerName
        End Get
        Set(ByVal value As String)
            _layerName = value
        End Set
    End Property

    Property workspace() As Object
        Get
            Return _workspace
        End Get
        Set(ByVal value As Object)
            _workspace = value
        End Set
    End Property

    Private Sub btnGetGeo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGetGeo.Click
        If geoForm IsNot Nothing AndAlso geoForm.Visible Then
            Return
        End If

        geoForm = New GeoLocation()
        geoForm.Show()
        geoForm.workspace = Me.workspace
        geoForm.oidParentText = oidValue

    End Sub
End Class